﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.customer.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.hh.bss.base.*;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.customer.dao.*;
import com.hh.bss.customer.entity.*;
import com.hh.bss.customer.service.*;
import com.hh.bss.customer.vo.query.*;
import com.hh.bss.project.service.SearchProjectCvService;
import com.hh.bss.talent.dao.CvContactDetailDao;
import com.hh.bss.talent.dao.CvDao;
import com.hh.bss.talent.dao.CvSalaryDao;
import com.hh.bss.talent.entity.Cv;
import com.hh.bss.talent.entity.CvContactDetail;
import com.hh.bss.talent.entity.CvSalary;
import com.hh.bss.talent.service.CvService;
import com.hh.bss.util.*;




@Service
@Transactional
public class CompanyCvServiceImpl extends BaseBusinessService<CompanyCv,java.lang.Integer>  implements CompanyCvService<CompanyCv,java.lang.Integer>{

	private CompanyCvDao companyCvDao;
	private CvDao cvDao;
	private CvSalaryDao cvSalaryDao;
	private SearchProjectCvService searchProjectCvService;
	private CvContactDetailDao cvContactDetailDao;
	
	
	private CustomerContacterService customerContacterService;
	
	@Autowired  //注解注入
	public void setCustomerContacterService(CustomerContacterService customerContacterService) {
		this.customerContacterService = customerContacterService;
	}
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setCompanyCvDaoImpl(CompanyCvDao dao) {
		this.companyCvDao = dao;
	}
	public void setCvSalaryDaoImpl(CvSalaryDao dao) {
		this.cvSalaryDao = dao;
	}
	private CompanyDeptTitleService companyDeptTitleService;
	private CompanyDeptService companyDeptService;
	private CompanyService companyService;
	private CvService cvService;
	public void setSearchProjectCvServiceImpl(SearchProjectCvService service) {
		this.searchProjectCvService = service;
	}
	public void setCompanyDeptServiceImpl(CompanyDeptService service) {
		this.companyDeptService = service;
	}
	public void setCvDaoImpl(CvDao dao) {
		this.cvDao = dao;
	}
	public void setCvContactDetailDaoImpl(CvContactDetailDao dao) {
		this.cvContactDetailDao = dao;
	}
	public void setCompanyDeptTitleServiceImpl(CompanyDeptTitleService service) {
		this.companyDeptTitleService = service;
	}
	public void setCompanyServiceImpl(CompanyService service) {
		this.companyService = service;
	}
	public void setCvServiceImpl(CvService service) {
		this.cvService = service;
	}
	public EntityDao getEntityDao() {
		return this.companyCvDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(CompanyCvQuery query) {
		return companyCvDao.findPage(query);
	}
	public Page findSearchProjectCompanyCvPage(CompanyCvQuery query) {
		// TODO Auto-generated method stub
		return companyCvDao.findSearchProjectCompanyCvPage(query);
	}
	public void save(CompanyCv companyCv){
		//如果已经存在则抛异常		
		Object[] os={companyCv.getCompanyId(),companyCv.getCvId()};
		List l=companyCvDao.getHibernateTemplate().find("from CompanyCv  pc where pc.companyId=? and pc.cvId=? ", os);
		Cv cv=cvService.getById(companyCv.getCvId());
		if (l!=null&&l.size()>0){
			//已经存在
			 throw new RuntimeException("人选["+cv.getCvName()+"]已经加入过公司。");
		}
		
		//设置人选现任公司
		//cv.setCompany(companyCv.getc);
		//查询公司
		Company company=(Company)companyService.getById(companyCv.getCompanyId());
		CompanyDept companyDept=null;
		CompanyDeptTitle companyDeptTitle=null;
		
		//设置人选现任公司
		//cv.setCompany(company.getName() == null ? company.getAttrName() : company.getName() );
		//cv.setCompanyId(company.getId());
		if (companyCv.getDeptId()!=null) companyDept=(CompanyDept)companyDeptService.getById(companyCv.getDeptId());
		
		if (companyCv.getTitleId()!=null) companyDeptTitle=(CompanyDeptTitle)companyDeptTitleService.getById(companyCv.getTitleId());
		
		//设置人选现任公司
		if (company!=null&&cv!=null) {
			cv.setCompany(company.getAbbrName());
			cv.setCompanyId(company.getId());
		}
		//设置人选当前所在地 和所在城市
		if(company.getLocation() != null && company.getLocation() > 0  ){
			cv.setCurrentAddress(company.getLocation().toString());
			cv.setRegisterLocation(company.getLocation());
			if(StringUtils.isEmpty(cv.getExpectLocation())){
				cv.setExpectLocation(company.getLocation().toString());
			}
		}
		if (companyDept!=null&&cv!=null) cv.setDept(companyDept.getName());
		if (companyDeptTitle!=null&&cv!=null) cv.setTitle(companyDeptTitle.getName());
		
		//如果没有岗位，取默认的
		if (companyCv.getDeptId()==null||companyCv.getDeptId()==0) companyCv.setDeptId(getDefaultDept(companyCv.getCompanyId()));
		if (companyCv.getTitleId()==null||companyCv.getTitleId()==0) companyCv.setTitleId(getDefaultTitle(companyCv.getCompanyId()));
		//没有默认则设置为0
		if (companyCv.getDeptId()==null) companyCv.setDeptId(0);
		if (companyCv.getTitleId()==null) companyCv.setTitleId(0);
		
		//设置人才的行业为公司的行业
		if(company.getIndustry()!=null&&((cv.getIndustry()==null)||cv.getIndustry()!=company.getIndustry())){
			cv.setIndustry(company.getIndustry());			
		}
		
		cvService.update(cv);
		//cvServic
		//companyCvDao.flush();
		//查询部门
		
		//查询岗位
		
		//查询人选
		//自动并设置cc项目目标公司人选
		searchProjectCvService.addProjectCvByCompanyCv(companyCv.getCompanyId());
		
		//cv.setCompany(value)
		companyCvDao.save(companyCv);
		companyCvDao.flush();
		
		
		//如果人选的联系类型 是客户公司的联系类型 自动关联到客户公司的联系人
		cvService.doCompanyContacter(cv);
		/*
		if(cv.getCvTag() !=null && cv.getCompanyId() >0 ){

			//根据公司Id 找到客户公司Id
			Integer custId  = customerContacterService.getCustIdByCompanyId(cv.getCompanyId());
			if(custId != null && cv.getCvType() != null ){
				//cv.cv_type从常量选择，人才类型：2020:BD联系人，2021:合作决策人，2022:职位联系人，
				//2023:收款联系人，2024:成功候选人（录入offer后更新），2025 ：普通候选人 
				//对应客户联系人 //CT-BD联系人  CP-合作决策人 JC-职位联系人  FC-收款联系人
				Map<Integer,String> cvTypeMap = new HashMap<Integer, String>();
				cvTypeMap.put(2020, "CT");  //BD联系人
				cvTypeMap.put(2021, "CP"); //合作决策人
				cvTypeMap.put(2022, "JC");  //职位联系人
				cvTypeMap.put(2023, "FC"); //收款联系人
				Integer  cvTypeCode = Integer.valueOf(cv.getCvType());
				if(cvTypeMap.containsKey(cvTypeCode)){
					//检查是否存在 同类型的客户联系人.
					//int count = customerContacterService.checkCustomerRoleCount(custId.toString(), cvTypeMap.get(cvTypeCode));
					//if(count<1 && count != -1){
						CustomerContacter  contacter = new CustomerContacter();
						contacter.setCreateDt(cv.getCreateDt());
						contacter.setCreator(cv.getCreator());
						contacter.setCvId(cv.getId());
						contacter.setRole(cvTypeMap.get(cvTypeCode));
						contacter.setCustId(custId);
						contacter.setComment("系统自动添加");
						customerContacterService.save(contacter); //客户联系人
					//}
				}
				
			}
	
		
			
		}
		*/
	}
	public List findCvCompany(CompanyCvQuery query) {
		// TODO Auto-generated method stub
		return companyCvDao.findCvCompany(query);
	}
	public Page findCompanyCv(CompanyCvQuery query) {
		// TODO Auto-generated method stub
		return companyCvDao.findCompanyCv(query);
	}
	private Integer getDefaultDept(Integer companyId){
		List values = new ArrayList();
		values.add(companyId);
		values.add("缺省部门");
		List l1=cvDao.getJdbcDao().findByQuery("select id from company_dept where company_id=? and name=?", values.toArray(),  new MapObjectRowMapper());
		Map m=null;
		Integer deptId=null;
		if (l1!=null&&l1.size()>0){
			m=(Map)l1.get(0);
			deptId=(Integer)m.get("id");
		}
		return deptId;
	}
	private Integer getDefaultTitle(Integer companyId){
		List values = new ArrayList();
		values.add(companyId);
		
		values.add("缺省岗位");
		Integer titleId=null;
		List l1=cvDao.getJdbcDao().findByQuery("select id from company_dept_title where company_id=? and name=?", values.toArray(),  new MapObjectRowMapper());
		if (l1!=null&&l1.size()>0){
			Map m=(Map)l1.get(0);
			titleId=(Integer)m.get("id");
		}	
		return titleId;
	}
	public void saveCompanyCvInfo(Cv cv, CompanyCv companyCv,CvSalary cvSalary,List cvContacts) {
		// TODO Auto-generated method stub
		
		//查询并设置人选的现任公司
		Company company=(Company)companyService.getById(companyCv.getCompanyId());
		CompanyDept companyDept=null;
		CompanyDeptTitle companyDeptTitle=null;
		
		
		if (companyCv.getDeptId()!=null) companyDept=(CompanyDept)companyDeptService.getById(companyCv.getDeptId());
		
		if (companyCv.getTitleId()!=null) companyDeptTitle=(CompanyDeptTitle)companyDeptTitleService.getById(companyCv.getTitleId());
		
		if (company!=null&&cv!=null) {
			cv.setCompany(company.getAbbrName());
			cv.setCompanyId(company.getId());
		}
		if (companyDept!=null&&cv!=null) cv.setDept(companyDept.getName());
		if (companyDeptTitle!=null&&cv!=null) cv.setTitle(companyDeptTitle.getName());
		
		
		cvDao.save(cv);
		cvDao.flush();
		
		//如果人选的联系类型 是客户公司的联系类型 自动关联到客户公司的联系人
		cvService.doCompanyContacter(cv);
		
		for(int i=0;cvContacts!=null&&i<cvContacts.size();i++){
			CvContactDetail cvContactDetail=(CvContactDetail)cvContacts.get(i);
			cvContactDetail.setCvId(cv.getId());
			cvContactDetailDao.save(cvContactDetail);
			cvContactDetailDao.flush();
		}
		if (cvSalary.getCurrentSalary()!=null){				
			cvSalary.setCvId(cv.getId());
			cvSalary.setCurrency("RMB");
			cvSalary.setExpectSalary(0);
			cvSalary.setCreateDt(new Date());
			cvSalary.setCreator(cv.getCreator());			
			cvSalaryDao.save(cvSalary);
			cvSalaryDao.flush();
		}
		
		companyCv.setCvId(cv.getId());
		companyCv.setCreator(cv.getCreator());
		companyCv.setCreateDt(new Date());
		companyCvDao.save(companyCv);
		companyCvDao.flush();
		
		//设置人才的行业为公司的行业		
		if(company.getIndustry()!=null&&((cv.getIndustry()==null)||cv.getIndustry()!=company.getIndustry())){
			cv.setIndustry(company.getIndustry());
			companyCvDao.update(cv);
			companyCvDao.flush();
		}
		//自动并设置cc项目目标公司人选
		searchProjectCvService.addProjectCvByCompanyCv(companyCv.getCompanyId());
		 //更新联系方式
		cvService.updateCvContact(cv.getId());
	}
	
}
